﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class menu
    {
        int _curturn;
        int _nbagents;
        Overmind _omind = new Overmind();
        List<agents> _agentlist = new List<agents>();
        stat st = new stat();
        private bool debug = false;


        public menu()
        {
            this._curturn = 0;
            this._nbagents = 0;
        }

        public void launchmenu()
        {
            Console.WriteLine("Module [IA]Intelligent Agents");
            Console.WriteLine("-------------------------R29-");
            Console.WriteLine("Menu de selection :");
            Console.WriteLine("[1] - Ajouter un agents");
            Console.WriteLine("[2] - Ajouter x agents");
            Console.WriteLine("[3] - Effectuer un tour");
            Console.WriteLine("[4] - Effectuer x tours");
            Console.WriteLine("[5] - Afficher les stats");
            Console.WriteLine("[6] - Sauvegarder le graph");
            Console.WriteLine("[7] - Debug On/Off");
            Console.WriteLine("[8] - Quitter le programme");
            Console.WriteLine("--------------Current Turn:" + this._curturn.ToString() + "-");
            Console.WriteLine("--------------Agents alive:" + this._nbagents.ToString() + "-");
            Console.WriteLine("-----The path of evolution:" + this._omind.Datalist.Count.ToString() + "-");
            int cans = Console.Read();
            int b;
            try
            {
                b = int.Parse(@Console.In.ReadLine());
                Console.WriteLine("");
            }
            catch (Exception e)
            {
                //Console.Out.Write(e.Message);
            }
            
            int answer = Convert.ToInt32(cans);

            if (answer == '1')
            {
                Console.WriteLine("[INFO] Adding 1 agent to the agent list");
                Console.WriteLine("Agent list currently contains : [" + this._nbagents + "] agent(s)");
                addAgents(1);
                launchmenu();
            }
            else if (answer == '2')
            {
                Console.WriteLine("How many agents do you wish to add ?");
                int nbag = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("[INFO] Adding " + nbag.ToString() + " agents to the agent list");
                Console.WriteLine("[INFO] Agent list currently containes : [" + this._nbagents + "] agent(s)");
                addAgents(nbag);
                launchmenu();
            }
            else if (answer == '3')
            {
                Console.WriteLine("[INFO] 1 new turn is about to start");
                Console.WriteLine("[INFO] Currently at turn [" + this._curturn + "]");
                maketurns(1);
                launchmenu();
            }
            else if (answer == '4')
            {
                Console.WriteLine("How many turns do you wish to launch ?");
                int nbag = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("[INFO] " + nbag + "new turns are about to start");
                Console.WriteLine("[INFO] Currently at turn [" + this._curturn + "]");
                maketurns(nbag);
                launchmenu();
            }
            else if (answer == '5')
            {
                Console.WriteLine("[INFO] Displaying current stats");
                st.affStat();
                launchmenu();
            }
            else if (answer == '6')
            {
                Console.WriteLine("[INFO] Saving charts");
                graph g = new graph(st._avgturnofdeath);
                launchmenu();
            }
            else if (answer == '7')
            {
                this.debug = !this.debug;
                this.activate_debug();
                launchmenu();
            }
            else if (answer == '8')
            {
                Console.WriteLine("[INFO] Exiting");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("[ERROR] Wrong input, please type a number between [1-6]");
                this.launchmenu();
            }
        }

        public void addAgents(int nbagents)
        {
            for (int i = 0; i < nbagents; i++)
            {
                agents temp = new agents(_omind);
                temp.setDebug(this.debug);
                _agentlist.Add(temp);
                this._nbagents += 1;
            }
        }

        public void activate_debug()
        {
            for (int i = 0; i < _agentlist.Count; ++i)
            {
                _agentlist[i].setDebug(this.debug);
            }
        }

        public void maketurns(int nbturns)
        {
            double avg = 0;
            int cpt = 0;
            Perf p = new Perf();

            for (int i = 0; (i < nbturns) && (_agentlist.Count != 0); i++)
            {
                for (int j = 0; j < _agentlist.Count; j++)
                {
                    if (_agentlist[j].isAlive())
                        _agentlist[j].apply_id3();
                    else
                    {
                        p += this._agentlist[j].p;
                        _agentlist.RemoveAt(j);
                        //Console.WriteLine("L'agent [" + j.ToString() + "] est mort au tour : " + this._curturn.ToString());
                        j--;
                        this._nbagents -= 1;

                        //avg += i;
                        cpt += 1;
                    }
                }
                this._curturn += 1;
            }
            if (_agentlist.Count() > 0)
            {
                //cpt += _agentlist.Count();
                //avg += nbturns * _agentlist.Count();
                for (int i = 0; i < _agentlist.Count(); i++)
                {
                    p += _agentlist[i].p;
                    cpt++;
                    _agentlist[i].agentdeath();
                }
                _agentlist.Clear();
                _nbagents = 0;
            }
            if (cpt > 0)
                st.addStat(p / cpt);
        }

        public void exitmenu(bool affimg)
        {
            if (affimg)
            {
                //afficher l'image
            }
        }
    }
}
